//
//  PictureViewController.m
//  BeHappy
//
//  Created by dlios on 15/3/9.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PictureViewController.h"

@interface PictureViewController ()

@end

@implementation PictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"乐看";
    self.allPicArray = [NSMutableArray array];
    
    self.picTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64) style:UITableViewStylePlain];
    self.picTableView.delegate = self;
    self.picTableView.dataSource = self;
    self.picTableView.backgroundColor = [UIColor colorWithRed:177.0/255 green:160.0/255 blue:151.0/255 alpha:1.0];
    [self.view addSubview:self.picTableView];

    
    
    NSString *urlStr = @"http://api.budejie.com/api/api_open.php?a=newlist&appname=baisibudejie&asid=64FD4D2D-91F4-4619-8089-29D43611F42B&c=data&client=iphone&device=iPhone%204&from=ios&jbk=1&mac=&market=&openudid=4690a8b3a9cc59296735340a3cdaad43d692fc3f&page=0&per=20&type=10&udid=&ver=3.2";
    DownloadTool *down = [[DownloadTool alloc] initWithUrlStr:urlStr];
    down.myDelegate = self;
    
}

- (void)didDownloadFinished:(id)result
{
//    NSLog(@"result = %@", result);
    NSDictionary *allDic = result;
    NSArray *listArray = [allDic objectForKey:@"list"];
    for (NSDictionary *dic in listArray) {
        PictureModel *picModel = [[PictureModel alloc] init];
        [picModel setValuesForKeysWithDictionary:dic];
        NSString *str = [NSString stringWithFormat:@"%@", [dic objectForKey:@"gifFistFrame"]];
        picModel.image0 = str;
        
        [self.allPicArray addObject:picModel];
    }
    [self.picTableView reloadData];
}


// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allPicArray.count;
}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureModel *picModel = [self.allPicArray objectAtIndex:indexPath.row];
    
    CGFloat picWidth = self.view.frame.size.width - 20; // 保证宽度一致
    CGFloat scale = picWidth / picModel.width; // 缩放比例
    CGFloat picHeight = (CGFloat)picModel.height * scale; // 保证比例不失调
    if (picHeight > 667.0) {
        picHeight = 300;
    }
    
    return picHeight + [PictureTableViewCell heightForText:picModel.text width:self.view.frame.size.width - 20] + 60 + 30;
}

// cell
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"reuse";
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[PictureTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.picModel = [self.allPicArray objectAtIndex:indexPath.row];

    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
